local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.GameObject());

local IsClient = game:GetService("RunService"):IsClient();

function M.new(self,...)
	assert(self, "param model is nil");
	return M(self,...);
end;

function M.ctor(self, data,sprite)
	self.playData=data
	M.super.ctor(self, sprite);
	self:Init()
end;
function M.Init(self)
	
end


function M.dtor(self)
	
	self:Stop()
	
	M.super.dtor(self);
end;


--data,frameBack,completeBack 播放数据，每帧回调，完成回调

function M.Play(self,frameBack,completeBack)
	
	if self.__updata_play_Connect then
		return;
	end;
	
	self.frameBack=frameBack
	self.completeBack=completeBack
	self.startTime=tick() 
	self.lastFrame = self.playData.lastFrame
	self.isLooping=false

	if self.playData.wait and self.playData.wait>0 then
			
		local transparency= self.main_inst.Transparency
		self.main_inst.Transparency=1
		delay(self.playData.wait,function()
			self.main_inst.Transparency=transparency
			self:PlayInit()
			if IsClient then
				self.__updata_play_Connect=self:AddRenderUpdate("render", self.UpdateFrame);
			else
				self.__updata_play_Connect=self:AddStepUpdate(self.UpdateFrame);
			end
		end)
		
	else
		self:PlayInit()
		if IsClient then
			self.__updata_play_Connect=self:AddRenderUpdate("render", self.UpdateFrame);
		else
			self.__updata_play_Connect=self:AddStepUpdate(self.UpdateFrame);
		end
	end
	

end

function M.PlayInit(self)
	
end

function M.Stop(self)
	if self.__updata_play_Connect then
		self.__updata_play_Connect:Disconnect();
		self.__updata_play_Connect = nil;
	end;
end

function M.SpriteUpdate(self,CurrentColumn,CurrentRow)
	
end


function M.Done(self)  --完成第一次播放
	
	if self.completeBack then
		self:AddProcess(function()
			self.completeBack(self)
		end)
	end

end

function M.DoneOneFrame(self,index) --每一帧回调
	
	if self.frameBack then
		self:AddProcess(function()
			self.frameBack(index)
		end)
	end
	
end


function M.UpdateFrame(self)
	if tick()-self.startTime >= 1/self.playData.warperFramerate then -- 大于下一帧时间，更新下一帧 check if enough time has passed between frames to update the sprite
		self.lastFrame = self.lastFrame + 1
		if self.isLooping and self.lastFrame>self.playData.loopEnd then
			self.lastFrame = self.playData.loopStart 
		end
		if self.lastFrame > self.playData.frames then --播放完毕
			self:Done()
			if self.playData.isLoop then
				self.lastFrame = self.playData.loopStart 
				self.isLooping=true
			else
				self:Stop()
				return true
			end
		end -- Loop gif

		--计算当前帧的行列
		local CurrentColumn = self.lastFrame -- set the current horizontal frame to the current frame
		local CurrentRow = 1
		repeat -- This gets what the current column and row should be based on the current frame (this is why we set CurrentColumn to self.lastFrame earlier)
			if CurrentColumn>self.playData.columns then
				CurrentColumn = CurrentColumn - self.playData.columns
				CurrentRow = CurrentRow + 1
			end
		until not(CurrentColumn>self.playData.columns)

		self:SpriteUpdate(CurrentColumn,CurrentRow)

		self:DoneOneFrame(self.lastFrame)

		self.startTime = tick()
	end
end

return M